public class Rotate {

    public void rotate(int[] nums, int k) {
        k = k % nums.length;
        int outIndex = 0;
        int index = 0;
        int left = nums[index];
        int count = 0;
        while (true) {
            int nextIndex = index + k < nums.length ? index + k : index + k - nums.length;
            int tmp = nums[nextIndex];
            nums[nextIndex] = left;
            if (++count == nums.length) {
                break;
            }
            left = tmp;
            index = nextIndex;
            if (index == outIndex) {
                outIndex++;
                index = outIndex;
                left = nums[outIndex];
            }
        }
    }

    public static void main(String[] args) {
        Rotate r = new Rotate();

        int[] nums = {-1, -100, 3, 99};
        r.rotate(nums, 2);
    }
}
